/*
 * @Author: 陈迪秀 1170776994@qq.com
 * @Date: 2024-02-28 09:02:15
 * @LastEditors: qf
 * @LastEditTime: 2024-04-19 16:24:03
 * @FilePath: \stell-mall\src\hooks\use-router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { ref, watch } from "vue";
import { useRouter } from "vue-router";
export const useWatchRouter = () => {
  const router = useRouter();
  const routerName = ref(""); // 路由名称
  const routerObjs = ref({}); // 路由对象
  const classARouter = ref<boolean>(true); // 是否是显示头部
  const suspensionShow = ref<boolean>(true);
  const classAArr = ref<Array<string>>([
    "home",
    "contract-fulfillment", // 履约保
    "intelligent-manufacturing",
    "dispatch-work-apply",
    "painting-apply",
    "install-apply",
    "about-us",
    "urge-secretary", // 督小秘
    "artisan-talent", // 匠才宝
    "wisdom-erp",
    "crm", //CRM
    "management-cockpit", //管理驾驶舱
    "technical-services", //技术服务
    "legal-consulting", // 法律咨询
    "labour-service", // 劳务服务
    "financial-tax-consult", // 财税咨询
    "system-certification", // 体系认证
  ]);
  const suspensionArr = ref<Array<string>>([
    "home",
    "contract-fulfillment",
    "intelligent-manufacturing",
    "software-mall",
    "steel-home",
    "about-us",
  ]);
  // 获取当前滚动位置的函数
  function getScrollPosition() {
    return window.pageYOffset || document.documentElement.scrollTop; // 支持不同浏览器
  }

  // 将页面滚动到指定位置的函数
  function scrollTo(position: any) {
    if (window.requestAnimationFrame) {
      const step = function () {
        window.scrollTo(0, position);
        if (getScrollPosition() !== position) {
          requestAnimationFrame(step);
        }
      };
      requestAnimationFrame(step);
    } else {
      window.scrollTo(0, position);
    }
  }

  // 点击按钮时调用该函数，将页面滚动到顶部
  function backToTop() {
    scrollTo(0);
  }
  // 监听当前路由
  watch(
    () => router.currentRoute.value,
    (newValue: any) => {
      routerObjs.value = newValue;
      routerName.value = newValue.name;
      classARouter.value = classAArr.value.includes(newValue.name);
      suspensionShow.value = suspensionArr.value.includes(newValue.name);
      backToTop();
    },
    { immediate: true }
  );
  return {
    routerName,
    routerObjs,
    classARouter,
    suspensionShow,
  };
};
